import core.ui.Event;

import xcms.DataBus;
import xcms.plugins.editor.EditorDialog;
import xcms.plugins.editor.Inspector;

class xcms.plugins.editor.EditorPlugin
{
    /**
     * Initializes the plugin.
     */
    this.EditorPlugin = function()
    {
        xcms.subscribe( 'onAuthComplete', this );
    }

    /**
     * Calls when authentication is completed.
     */
    this.onAuthComplete = function()
    {
        this.inspector = new Inspector();
        this.inspector.addSelectionListener(this);
        this.inspector.setInspecting(true);
    }

    /**
     * Creates if needed and returns the editor dialog.
     * @return Singleton instance of the editor dialog. 
     */
    this.getEditorDialog = function()
    {
        if ( !defined( this.dialog ) )
        {
            this.dialog = new EditorDialog();
            dialog.addDialogListener( this );
        }
        return this.dialog; 
    }

    /**
     * Calls when user selects the particular page element.
     *
     * @param inspector (Inspector) Inspector object.
     */
    this.onElementSelected = function( inspector )
    {
        console.log('Element selected: ' + inspector.getSelectedElement());
        // TODO: display dialog 
    }

    this.onDialogComplete = function( dialog )
    {
        // TODO: replace element HTML
        dialog.endModal();
    }

    this.onDialogCancelled = function( dialog )
    {
        dialog.endModal();
    }
}